<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_ProfessionalTitleTypeId_FormItem"
								prop="Entity.ProfessionalTitleTypeId"
								:rules="[{ required: true, message: '资质类别为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="资质类别" tooltip="关联的职称类别" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.ProfessionalTitleTypeId" disabled class="width100">
									<el-option
										v-for="item in stateData.AllSysPersonProfessionalTitleTypes"
										:key="item.Value"
										:value="item.Value"
										:label="item.Text"
									></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>资质信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_QualificationName_FormItem"
								prop="Entity.QualificationName"
								:rules="[{ required: true, message: '资质名称为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="资质名称" tooltip="资质的具体名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.QualificationName" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_QualificationLevel_FormItem" prop="Entity.QualificationLevel">
								<template #label>
									<WtmLabelTooltip label="资质等级" tooltip="资质等级（如：国家级/省级/市级/区县级）" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.QualificationLevel" disabled class="width100">
									<el-option key="高级" value="高级" label="高级"></el-option>
									<el-option key="中级" value="中级" label="中级"></el-option>
									<el-option key="初级" value="初级" label="初级"></el-option>
									<el-option key="国家级" value="国家级" label="国家级"></el-option>
									<el-option key="省级" value="省级" label="省级"></el-option>
									<el-option key="市级" value="市级" label="市级"></el-option>
									<el-option key="区县级" value="区县级" label="区县级"></el-option>
									<el-option key="正高级" value="正高级" label="正高级"></el-option>
									<el-option key="副高级" value="副高级" label="副高级"></el-option>
									<el-option key="助理级" value="助理级" label="助理级"></el-option>
									<el-option key="员级" value="员级" label="员级"></el-option>
									<el-option key="特级" value="特级" label="特级"></el-option>
									<el-option key="一级" value="一级" label="一级"></el-option>
									<el-option key="二级" value="二级" label="二级"></el-option>
									<el-option key="三级" value="三级" label="三级"></el-option>
									<el-option key="四级" value="四级" label="四级"></el-option>
									<el-option key="五级" value="五级" label="五级"></el-option>
									<el-option key="其他" value="其他" label="其他"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_IssuingAuthority_FormItem" prop="Entity.IssuingAuthority">
								<template #label>
									<WtmLabelTooltip label="发证单位" tooltip="颁发证书的机构" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.IssuingAuthority" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_CertificateCode_FormItem" prop="Entity.CertificateCode">
								<template #label>
									<WtmLabelTooltip label="证书编码" tooltip="证书的唯一编码" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.CertificateCode" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ObtainedDate_FormItem" prop="Entity.ObtainedDate">
								<template #label>
									<WtmLabelTooltip label="获得日期" tooltip="获得资质的日期" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.ObtainedDate" disabled class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ExpiryDate_FormItem" prop="Entity.ExpiryDate">
								<template #label>
									<WtmLabelTooltip label="有效期" tooltip="资质的有效期" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.ExpiryDate" disabled class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<!-- <el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SalaryReward_FormItem" prop="Entity.SalaryReward">
								<template #label>
									<WtmLabelTooltip label="薪资奖励" tooltip="与资质相关的薪资或奖励信息" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.SalaryReward" disabled class="width100"></el-input-number>
							</el-form-item>
						</el-col> -->
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_CertificateDocumentId_FormItem" prop="Entity.CertificateDocumentId">
								<template #label>
									<WtmLabelTooltip label="证书文件" tooltip="资质相关的证明材料" />
								</template>
								<wtm-upload-file v-model="stateData.vmModel.Entity.CertificateDocumentId" disabled :filePreviewCount="1"></wtm-upload-file>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Remark_FormItem" prop="Entity.Remark">
								<template #label>
									<WtmLabelTooltip label="备注" tooltip="额外说明信息" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Remark" type="textarea" :rows="3" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysPersonProfessionalTitleApi as api } from '/@/views/person/syspersonprofessionaltitle/api';
import other from '/@/utils/other';
import { ISysPersonProfessionalTitleVm } from '/@/views/person/syspersonprofessionaltitle/api/interface';
import { ISysSelectType, IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysPersonProfessionalTitleVm>>({
	vmModel: {
		Entity: {
			ID: null,
			PersonId: null,
			ProfessionalTitleTypeId: null,
			QualificationName: null,
			QualificationLevel: null,
			IssuingAuthority: null,
			CertificateCode: null,
			ObtainedDate: null,
			ExpiryDate: null,
			SalaryReward: null,
			CertificateDocumentId: null,
			Remark: null,
		},

		ActionName: null,
		IsMyApprove: null,
	},
	AllSysPersons: [] as ISysSelectType[],
	AllSysPersonProfessionalTitleTypes: [] as ISysSelectType[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		other.getSelectList('/api/SysPersonProfessionalTitle/GetSysPersons', [], false).then((x) => (stateData.AllSysPersons = x)),
		other
			.getSelectList('/api/SysPersonProfessionalTitle/GetSysPersonProfessionalTitleTypes', [], false)
			.then((x) => (stateData.AllSysPersonProfessionalTitleTypes = x)),
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
